package com.yami.shop.api.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/p/heart")
@Api(tags = "心跳接口")
@AllArgsConstructor
public class HeartbeatController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

        @GetMapping("/upHeart")
        @ApiOperation(value = "更新心跳时间", notes = "更新心跳时间")
        public void upHeartbeat(){

            String userId = SecurityUtils.getUser().getUserId();
            if(userId != null && userId != ""){
                Date now = new Date();
                userMapper.editIsOnlz(userId);
            }

            /*List<User> lists= new ArrayList<>();
            lists = RedisUtil.get("allUser");
            String pd = "0";
            for(User list : lists){
                if(list.getUserId().equals(userId)){
                    if(list.getIsOnline().equals(0)){
                        list.setIsOnline(1);
                    }
                    list.setHeartbeat(now);
                    RedisUtil.del("allUser");
                    RedisUtil.set("allUser",lists);
                    pd = "1";
                }
            }

            if(pd.equals("0")){
                RedisUtil.del("allUser");
                List<User> allUser = userMapper.getallUser();
                RedisUtil.set("allUser",allUser);
                //List<User> lists= new ArrayList<>();
                lists = RedisUtil.get("allUser");
                for(User list : lists){
                    if(list.getUserId().equals(userId)){
                        if(list.getIsOnline().equals(0)){
                            list.setIsOnline(1);
                        }
                        list.setHeartbeat(now);
                        RedisUtil.del("allUser");
                        RedisUtil.set("allUser",lists);
                    }
                }
            }*/



        }

}
